package com.billdowney.qq.robot.controller;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

@Api(tags = "文件")
@RestController
@RequestMapping("/file")
public class FileUpload {
        String 机器人包="qq-robot-1.1.0.jar";
        String 机器人包存放位置="C:/qq机器人服务器部署包/后台服务器/";
        String 机器人包下载路径 = "http://124.220.209.191:9998/qq-robot-1.1.0.jar";

        String 脚本存放位置="C:/qq机器人服务器部署包/";
        String 脚本名称 = "启动qq机器人.bat";

        String 脚本下载路径 = "http://124.220.209.191:9998/启动qq机器人.bat";
    @GetMapping("/downloadFile")
    public void 机器人下载包() throws IOException {
        URL url = new URL(机器人包下载路径);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod(con.getRequestMethod());
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        con.setRequestProperty("User-Agent", "Mozilla/4.76");
        con.setRequestProperty("connection", "keep-alive");
        con.setDoOutput(true);
        BufferedInputStream out = new BufferedInputStream(con.getInputStream());
        File file = new File(机器人包存放位置+机器人包);
        file.createNewFile();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(机器人包存放位置+机器人包));
        try {
            int bytes = 0;
            byte[] bufferOut = new byte[1024];
            while ((bytes = out.read(bufferOut)) != -1) {
                bos.write(bufferOut, 0, bytes);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            out.close();
            bos.close();
        }
    }

    public void 脚本下载() throws IOException {
        URL url = new URL(脚本下载路径);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod(con.getRequestMethod());
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        con.setRequestProperty("User-Agent", "Mozilla/4.76");
        con.setRequestProperty("connection", "keep-alive");
        con.setDoOutput(true);
        BufferedInputStream out = new BufferedInputStream(con.getInputStream());
        File file = new File(脚本存放位置+脚本名称);
        file.createNewFile();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(脚本存放位置+脚本名称));
        try {
            int bytes = 0;
            byte[] bufferOut = new byte[1024];
            while ((bytes = out.read(bufferOut)) != -1) {
                bos.write(bufferOut, 0, bytes);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            out.close();
            bos.close();
        }
    }

}
